﻿#define MySql
#undef MSSql

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


namespace ukeasbjWeb
{
    
    public partial class _Default : Bonus ///System.Web.UI.Page
    {

        //浏览器ip地址
        string sIP;

        protected void Page_Load(object sender, EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            sIP = Request.UserHostAddress;
            Literal_ip.Text = "您的IP地址是:" + sIP + "  ";

            System.DateTime nowDateTime = System.DateTime.Now;

            Literal_TimeBeijing.Text = "今天是:" + nowDateTime.ToLocalTime().ToLongDateString() + "  北京时间:" + nowDateTime.ToLocalTime().ToLongTimeString();

            if (!IsPostBack)
            {
                Common_SXD.Kill_Sleep_Threads(Response);
                // 初始化页面
                Common_SXD.Init_E2C_Office(Response);
                Common_SXD.Init_Column_Translation(Response);
                // 读取管理员邮箱密码
                //Common_SXD.pwd_jianwei_rong = Common_SXD.Read_lines("C:/Users/rjw/password.txt", 0);
                Common_SXD.pwd_jianwei_rong = Common_SXD.Read_line_pwd("pwd", 0);
                
            }

            ///暂时关闭，发送otp 的功能
            ///Bt_Send_OTP.Enabled = false;

        }

        private void Illegal_User()
        {
            Session["Email"] = "";
            Session["Password"] = "";
            Session["Office"] = "";
            Session["Is_Counsellor"] = 0;
            Session["Is_Writer"] = 0;
            Session["Is_Active"] = 0;
            Session["Is_Admin"] = 0;
            Session["Is_G_M"] = 0;
            Session["Is_O_M"] = 0;
            Session["Is_Accountant"] = 0;
            Session["Is_Cashier"] = 0;
            Session["Is_Collector"] = 0;
            Session["Is_Receptionist"] = 0;
            Session["Changed_PW"] = 0;

        }

        private void Default_Page(string mysEmail, DataTable mydtCust)
        {
            Session["Email"] = mysEmail;
            Session["C_N_Employee"] = mydtCust.Rows[0]["C_N_Employee"].ToString().Trim();
            Session["Office"] = mydtCust.Rows[0]["Office"].ToString().Trim();
            Session["Password"] = mydtCust.Rows[0]["Password"].ToString().Trim();
            Session["Is_Counsellor"] = mydtCust.Rows[0]["Is_Counsellor"];
            Session["Is_Writer"] = mydtCust.Rows[0]["Is_Writer"];
            Session["Is_Active"] = mydtCust.Rows[0]["Is_Active"];
            Session["Is_Admin"] = mydtCust.Rows[0]["Is_Admin"];
            Session["Is_G_M"] = mydtCust.Rows[0]["Is_G_M"];
            Session["Is_O_M"] = mydtCust.Rows[0]["Is_O_M"];
            Session["Is_Accountant"] = mydtCust.Rows[0]["Is_Accountant"];
            Session["Is_Cashier"] = mydtCust.Rows[0]["Is_Cashier"];
            Session["Is_Collector"] = mydtCust.Rows[0]["Is_Collector"];
            Session["Is_Receptionist"] = mydtCust.Rows[0]["Is_Receptionist"];
            Session["Changed_PW"] = mydtCust.Rows[0]["Changed_PW"];
            Session["Is_HR"] = mydtCust.Rows[0]["Is_HR"];
            Session["Is_Marketing"] = mydtCust.Rows[0]["Is_Marketing"];
            Session["Is_Frontline_Agent"] = mydtCust.Rows[0]["Is_Frontline_Agent"];

            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_Collector = Convert.ToBoolean(Session["Is_Collector"]);
            bool bIs_Accountant = Convert.ToBoolean(Session["Is_Accountant"]);
            bool bIs_Receptionist = Convert.ToBoolean(Session["Is_Receptionist"]);
            bool bIs_Writer = Convert.ToBoolean(Session["Is_Writer"]);
            bool bIs_Counsellor = Convert.ToBoolean(Session["Is_Counsellor"]);

            bool bIs_G_M = Convert.ToBoolean(Session["Is_G_M"]);

            bool bIs_HR = Convert.ToBoolean(Session["Is_HR"]);
            bool bIs_Marketing = Convert.ToBoolean(Session["Is_Marketing"]);
                    

            bool bIs_Frontline_Agent = Convert.ToBoolean(Session["Is_Frontline_Agent"]);

            if (bIs_Collector)
                Response.Redirect("./CheckingCashFlow.aspx?Year=15"); 	// 默认的新资金流向表的年份
            else if (bIs_Counsellor || bIs_Writer )
                Response.Redirect("./CheckedCashFlow.aspx");
            else if (bIs_Frontline_Agent || bIs_Receptionist || bIs_Marketing)
                Response.Redirect("./Leads.aspx");
            else if (bIs_G_M )
                Response.Redirect("./ContractMan.aspx");
            else
                Response.Redirect("./InforBeijing.aspx");

                    

        }

        protected void Bt_Login_Click(object sender, EventArgs e)
        {
            //校验用户是否有效
            string sEmail, sUserPwd;
            sEmail = TextBox_UserID.Text.Trim(); // 读入用户输入的用户名和密码
            sUserPwd = TextBox_UserPwd.Text.Trim();
//////////////////////////////////////////////////////////////

            String sSql = "select * from Employees  where Email ='" + sEmail + "'"; // 根据输入的用户名和密码进行查询
#if MySql
            DataSet bsDataSet =  Common_SXD.Init_MySql_DataSet("Employees", sSql);
#elif MSSql
            DataSet bsDataSet =  Common_SXD.Init_MSSql_DataSet("Employees", sSql);
#endif
            DataTable dtCust = bsDataSet.Tables["Employees"];
            bool bIs_MOTP;
            bool bIs_Active = false;
            try
            {
                Session["Is_MOTP"] = dtCust.Rows[0]["Is_MOTP"];
                if (Session["Is_MOTP"].ToString().Length == 0)
                    bIs_MOTP = false;
                else
                    bIs_MOTP = Convert.ToBoolean(Session["Is_MOTP"]);

                Session["Is_Active"] = dtCust.Rows[0]["Is_Active"];
                if (Session["Is_Active"].ToString().Length == 0)
                    bIs_Active = false;
                else
                    bIs_Active = Convert.ToBoolean(Session["Is_Active"]);


            }
            catch (IndexOutOfRangeException error)
            {
                string temp = error.ToString();
                bIs_MOTP = false;
            }

            if ((dtCust.Rows.Count == 1) && !bIs_MOTP && bIs_Active )  //(sEmail != "jianwei_rong") )
            { ///eMail 存在，不采用 mobil otp 密码验证

                if (sUserPwd == dtCust.Rows[0]["Password"].ToString().Trim())
                {
                    Default_Page(sEmail, dtCust);
                
                }
                else
                {// 非法用户
                    Label_Msg.Text = "用户名或密码错误！";
                    Illegal_User();

                }
                
            }
            else
            {/// 用户名不存在或者用户名存在，采用mobil otp 密码
             /// 
                string sTemp;
                try
                {
                    string strPin = dtCust.Rows[0]["Pin"].ToString().Trim();
                    string strInitNumber = dtCust.Rows[0]["InitNumber"].ToString().Trim();

                    sTemp = Common_SXD.checkOTP(strPin, sUserPwd, strInitNumber);
                    if ((sUserPwd == sTemp) && (sTemp != ""))
                    {/// mobil otp 密码 OK
                        Default_Page(sEmail, dtCust);
                        
                    }
                    else
                    { // 用户名或 One Time Password 错误
                        Label_Msg.Text = "用户名或 One Time Password 错误！";
                        Illegal_User();

                    }
                }
                catch (IndexOutOfRangeException error)
                {
                    string temp = error.ToString();
                    // 用户名或 One Time Password 错误
                    Label_Msg.Text = "用户名或 One Time Password 错误！";
                    Illegal_User();
                    
                }
            }

        }
        
        /// <summary>
        /// 生成新的OTP密码,发送到登录用户的邮箱
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Bt_Send_OTP_Click(object sender, EventArgs e)
        {
            /// 从数据库中读取密码，发送到用户邮箱 
            Common_SXD.Sendmail_OTP_Password(TextBox_UserID.Text.Trim(), Response);
 
        }

    }
}
